<?php
class entry_edit {
    var $db = null;
    var $smarty = null;
    var $msg = null;
	
	function entry_edit() {
        $this->smarty = new MySmarty();
        $this->db = new DbAccess();
    }
	
	function displayForm($formvars = array())
	{
		$entry_id = $formvars['entry_id'];
		$sql = "SELECT * FROM entrylist WHERE entry_id = :entry_id";
		$stmt = $this->db->prepare($sql);
		$stmt->bindParam(':entry_id', $entry_id);
		$stmt->execute();
		$result = $stmt->fetch();
		$stmt = null;
		if($result != null)
		{
			$this->smarty->assign('rslt', $result);
			$this->getTagList($result['lang_id']);
			$this->getEntryTagList($result['entry_id']);
		}
		else
		{
			$this->smarty->assign('fatal', true);
			$this->msg="エントリ[$entry_id]が見つかりません";
		}
		
		$this->initDropDown();
		$this->smarty->assign('formvars',$formvars);
		$this->smarty->assign('msg', $this->msg);
		$this->smarty->display( 'entry_edit.tpl' );
	}
	
	
	function doUpdate($formvars = array())
	{
		$entry_id = $formvars['entry_id'];
		//echo $entry_id ;
		$sql = "Update entrylist set title='".$formvars['entry_title']."',"
		."entry_url='".$formvars['entry_url']."',comment='".$formvars['comment']."',"
		."description='".$formvars['description']."',open_flag=".$formvars['open_flag']
		.",up_date='".$formvars['up_date']."' where entry_id = $entry_id";
		//echo $sql ;
		$count =$this->db->exec($sql);
		if($count>0)
		{
			$this->msg = "エントリ[$entry_id]の更新に成功しました";
		}
		else
		{
			$this->msg = "エントリ[$entry_id]の更新に失敗しました";
		}
		
	}

	function getTagList($lang_id)
	{
		$sql = "SELECT * FROM tag_mst where lang_id=$lang_id order by tag_id";
		foreach ($this->db->query($sql) as $row) {
    		$resultArr[$row['tag_id']]=$row['tag_name'];
		}
		$this->smarty->assign('tagList', $resultArr);
	}

	function getEntryTagList($entry_id)
	{
		$sql = "SELECT tag_list.entry_id,tag_list.tag_id,tag_mst.tag_name
		        FROM tag_list 
				  inner join tag_mst on tag_list.tag_id = tag_mst.tag_id
				where tag_list.entry_id=$entry_id
				order by tag_list.tag_id ";
		$stmt = $this->db->prepare($sql);
		$stmt->execute();
		$resultArr = $stmt->fetchAll();
		$stmt = null;
		$this->smarty->assign('entryTabList', $resultArr);
		$this->smarty->assign('tagNum', sizeof($resultArr));
	}

	function delTag($formvars = array())
	{
		$entry_id = $formvars['entry_id'];
		$tag_id = $formvars['tag_id'];
		$count =$this->db->exec("DELETE FROM tag_list WHERE entry_id = '$entry_id' and tag_id='$tag_id'");
		if($count>0)
		{
			$this->msg = "エントリ[$entry_id]のタグ[$tag_id]を削除しました";
		}
		else
		{
			$this->msg = "エントリ[$entry_id]のタグ[$tag_id]の削除に失敗しました";
		}
	}

	function updateTag($formvars = array())
	{
		$entry_id = $formvars['entry_id'];
		$tagIdList=$formvars["tag_id"];
		
		$count =$this->db->exec("DELETE FROM tag_list WHERE entry_id = '$entry_id'");
		

		$idList = "";
		foreach($tagIdList as $key =>$id)
		{
				$idList.=$key==0?$id:",".$id;
		}
		if($idList == "")
		{
			return;
		}
		$idList = "(".$idList.")";

		$sql = "Insert into tag_list(entry_id,tag_id,category_id) 
			 select $entry_id,tag_id,category_id
			 from tag_mst 
			 where tag_id in $idList";
		//echo $sql;
		$count = $this->db->exec($sql);

		if($count>0)
		{
			$this->msg = "関連タグを更新しました";
		}
		else
		{
			$this->msg = "関連タグを更新できません";
		}
	}

	function insertTag($formvars = array())
	{
		$entry_id = $formvars['entry_id'];
		$tag_id = $formvars['tag_id'];
	//	$sql = "Insert into tag_list(entry_id,tag_id) 
	//		    Values($entry_id,$tag_id)";
		$sql = "Insert into tag_list(entry_id,tag_id,category_id) 
			 select $entry_id,tag_id,category_id
			 from tag_mst 
			 where tag_id = $tag_id";
	    //echo $sql;
		$count =$this->db->exec($sql);
		if($count>0)
		{
			$this->msg = "関連タグの追加に成功しました";
		}
		else
		{
			$this->msg = "関連タグの追加に失敗しました";
		}
	}
	
	function initDropDown()
	{	
		$this->smarty->assign('statusList', array(
                                0 => 'CLOSE',
                                1 => 'OPEN'));
	}	
}
?>